package com.peak.spring.boot.tools;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * Date类型操作
 * 
 * @author haley
 *
 */
public class Dates {
	
	/**
	 * 当前时间
	 * 
	 * @return
	 */
	public static Date now() {
		return new Date();
	}
	
	/**
	 * 下一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getNextDay(Date date) {
		return addDay(date, 1);
	}
	
	/**
	 * 前一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getPreDay(Date date) {
		return addDay(date, -1);
	}
	
	/**
	 * 移动多少分钟
	 * 
	 * @param date
	 * @param hours
	 * @return
	 */
	public static Date addMinute(Date date, int minutes) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, minutes);
		return cal.getTime();
	}
	
	/**
	 * 移动多少小时
	 * 
	 * @param date
	 * @param hours
	 * @return
	 */
	public static Date addHour(Date date, int hours) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, hours);
		return cal.getTime();
	}

	/**
	 * 移动多少天，负数前移
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDay(Date date, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, days);
		return cal.getTime();
	}
	
	/**
	 * 移动多少周，负数前移
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addWeek(Date date, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.WEEK_OF_YEAR, days);
		return cal.getTime();
	}
	
	/**
	 * 移动多少月，负数前移 如果移动后的月天数最后一天小于传入的日期的天，按移动后的最后一天算
	 * 
	 * @param date
	 * @param months
	 * @return
	 */
	public static Date addMonth(Date date, int months) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, months);
		return cal.getTime();
	}
	
	/**
	 * 移动多少年，负数前移
	 * 
	 * @param date
	 * @param years
	 * @return
	 */
	public static Date addYear(Date date, int years) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, years);
		return cal.getTime();
	}
	
	/**
	 * 清除时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date clearTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 设置23点59分59秒
	 * 
	 * @param date
	 * @return
	 */
	public static Date lastScend(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}

	/**
	 * 格式化，默认yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static String format(Date date) {
		return DateFormatUtils.format(date, DatePattern.SHORT_LINE.getPartten());
	}
	
	/**
	 * 格式化
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, DatePattern pattern) {
		return DateFormatUtils.format(date, pattern.getPartten());
	}
	
	/**
	 * 日期比较，小于等于
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean beforeAndEquals(Date date1, Date date2) {
		return date1.compareTo(date2) <= 0;
	}
	
	/**
	 * 日期比较，大于等于
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean afterAndEquals(Date date1, Date date2) {
		return date1.compareTo(date2) >= 0;
	}
	
	/**
	 * 日期比较，是否为同一天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Date date1, Date date2) {
		return format(date1).equals(format(date2));
	}
	
	/**
	 * 本周第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek() {
		Calendar cal = Calendar.getInstance();
		Date now = now();
		clearTime(now);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}

	/**
	 * 本周最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek() {
		Calendar cal = Calendar.getInstance();
		Date now = now();
		lastScend(now);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return cal.getTime();
	}
	
	/**
	 * 本月最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth() {
		Calendar cal = Calendar.getInstance();
		Date now = now();
		cal.setTime(now);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return cal.getTime();
	}
	
	/**
	 * 月最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return cal.getTime();
	}

	public static void main(String[] args) {
		System.out.println(format(addMonth(Converts.toDate("2015-03-31"), -1)));
		System.out.println(format(Dates.clearTime(new Date()), DatePattern.SHORT_LINE_TIME));
		System.out.println(format(Dates.lastScend(new Date()), DatePattern.SHORT_LINE_TIME));
	}
}
